package com.atguigu.gmall.weball.interceptor;

import com.atguigu.gmall.common.constant.RedisConst;
import com.atguigu.gmall.common.utils.RequestUtils;
import com.atguigu.gmall.weball.controller.CartController;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName UserAuthFeignInterceptor
 * @Description TODO feign发起远程调用的时候工作
 * @Author king
 * @Date 2022/11/17 8:56
 * @Version 1.0
 */
@Component
@Slf4j
public class UserAuthFeignInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        log.info("feign请求拦截介入...");

        //从头到尾都是一个线程======除非响应式编程,或者自己开新的线程
        HttpServletRequest request = RequestUtils.getRequest();

        String uid = request.getHeader(RedisConst.UID_HEADER);
        String tempid = request.getHeader(RedisConst.U_TEMP_ID_HEADER);

        //拿到网关放给web-all的老请求
        requestTemplate.header(RedisConst.UID_HEADER, uid);
        requestTemplate.header(RedisConst.U_TEMP_ID_HEADER, tempid);
    }
}
